﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YY.Common;
using YY.UI.BaseDataSvc;
using System.Linq;

namespace YY.UI
{
    public partial class BalanceFrm : YY.UI.BaseFrm
    {
        public BalanceFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataSvc.BaseDataClient();
            YY.Common.EventCommon.BalanceUdDGVEvnt += new YY.Common.EventCommon.UpdateDataGridViewDlgt(updateDgv);
        }

        private void BalanceFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            initDgvColumns();
            init();
            search();
        }

        void init()
        {
            this.dtpEDate.Value = this.dtpEDate.Value.Date.AddDays(1).Date.AddSeconds(-1);
            this.dtpSDate.Value = this.dtpSDate.Value.Date.AddDays(-7);
            //Show the SummaryBox
            this.dataGridViewSummary1.SummaryRowVisible = true;
            Common.BindEnumToComboBox(this.cbBoxBalanceType, typeof(EnumCommon.FeeType), null);
            this.cbBoxBalanceType.DropDownStyle = ComboBoxStyle.DropDownList;
            IEnumerable<TB_Clients> clients = this.BaseDataProxy.FindClients(null, null, null, null, null);
            Common.ComboBoxBindDatas<TB_Clients>(this.cbBoxTarget, clients, "fullName", "id");
        }

        /// <summary>
        /// 初始化表格列
        /// </summary>
        private void initDgvColumns()
        {
            this.dataGridViewSummary1.AutoGenerateColumns = true;
            //this.dataGridViewSummary1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;//AutoSize十分影响性能
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "id", "id", false, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "bType", "收支", true, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "bDate", "日期", true, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "target", "收支对象", true, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "amount", "金额", true, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "bank", "银行", true, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "desc", "描述", true, true, null);
            //Common.AddDgvColumn(this.dataGridViewSummary1, new DataGridViewTextBoxColumn(), "memo", "备注", true, true, null);
            this.dataGridViewSummary1.SummaryColumns = new string[] { "amount" };
            this.dataGridViewSummary1.SummaryRowVisible = true;  
        }

        void updateDgv(object o, EventArgs arg)
        { search(); }

        void search()
        {
            int? target = null;
            if (this.cbBoxTarget.SelectedItem != null)
                target = (this.cbBoxTarget.SelectedItem as TB_Clients).id;
            int? balanceType = null;
            if (this.cbBoxBalanceType.SelectedItem != null)
                balanceType = (int)Enum.Parse(typeof(EnumCommon.FeeType), this.cbBoxBalanceType.Text);
            IEnumerable<TB_Balance> balances = this.BaseDataProxy.FindBalances(this.dtpSDate.Value, this.dtpEDate.Value, target, balanceType, this.txtBoxBank.Text);
            this.dataGridViewSummary1.DataSource = balances.ToDataTable();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EditBalanceFrm frm = new EditBalanceFrm();
            frm.EditMode = false;
            if (this.cbBoxBalanceType.SelectedItem != null)
                frm.BalanceType = (EnumCommon.FeeType)Enum.Parse(typeof(EnumCommon.FeeType), this.cbBoxBalanceType.Text);
            frm.ShowDialog(this);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            search();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Common.ResetControl(this.groupBox1, null);
        }

        private void dataGridViewSummary1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditBalanceFrm frm = new EditBalanceFrm();
            frm.Id = Convert.ToInt32(this.dataGridViewSummary1.CurrentRow.Cells["id"].Value);
            frm.EditMode = true;
            frm.BalanceType = (EnumCommon.FeeType)Enum.Parse(typeof(EnumCommon.FeeType), this.dataGridViewSummary1.CurrentRow.Cells["bType"].Value.ToString());
            frm.ShowDialog(this);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            string dates = this.dtpSDate.Value.ToString("yyyy-MM-dd") + " 至 " + this.dtpEDate.Value.ToString("yyyy-MM-dd"); ;
            ExcelCommon2.ExlCell ecl1 = new ExcelCommon2.ExlCell("收支情况", 1, 1, 1, this.dataGridViewSummary1.Columns.Count, new ExcelCommon2.ExlCellStyle("宋体", 18, true, Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false));
            ExcelCommon2.ExlCell ecl2 = new ExcelCommon2.ExlCell(dates, 2, 1, 2, this.dataGridViewSummary1.Columns.Count, new ExcelCommon2.ExlCellStyle("宋体", 12, true, Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false));            
            //ExcelCommon2.ExlCell ecl3 = new ExcelCommon2.ExlCell("收支情况", 1, 1, 1, this.dataGridViewSummary1.Columns.Count, new ExcelCommon2.ExlCellStyle("宋体", 18, true, Microsoft.Office.Interop.Excel.Constants.xlColor3, Microsoft.Office.Interop.Excel.Constants.xlCenter, false));
            new ExcelCommon2().ExportDataGridViewToExcel("收支情况", this.dataGridViewSummary1, new List<ExcelCommon2.ExlCell>() { ecl1, ecl2 }, null);
        }

        private void cbBoxBalanceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool show = false;
            if (this.cbBoxBalanceType.SelectedItem != null)
                if (this.cbBoxBalanceType.SelectedItem.ToString() == EnumCommon.FeeType.应收.ToString())
                    show = true;
            this.label2.Visible = show;
            this.cbBoxTarget.Visible = show;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewSummary1.CurrentRow != null)
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (this.BaseDataProxy.DeleteBalance(Convert.ToInt32(this.dataGridViewSummary1.CurrentRow.Cells["id"].Value)))
                        this.dataGridViewSummary1.Rows.Remove(this.dataGridViewSummary1.CurrentRow);
                    else
                        MessageBox.Show("删除异常！");
                }
            }
        }

        /// <summary>
        /// 数据源删除选中行
        /// </summary>
        private void deleteRow(bool serverDelete, int id)
        {
            if (serverDelete)
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (!this.BaseDataProxy.DeleteBalance(id))
                        MessageBox.Show("删除异常！");
                }

            }
        }

        private void dataGridViewSummary1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            //Common.ShowDataGridViewRowNo(sender);
        }

        private void dataGridViewSummary1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            //Common.ShowDataGridViewRowNo(sender);
        }

        private void dataGridViewSummary1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            IEnumerable<TB_Clients> clients = this.BaseDataProxy.FindClients(null, null, null, null, null);
            foreach (DataGridViewRow dgvRow in this.dataGridViewSummary1.Rows)
            {
                if (!Common.IsDgvCellNull(dgvRow.Cells["bType"].Value))
                    dgvRow.Cells["bTypeName"].Value = Enum.GetName(typeof(EnumCommon.FeeType), dgvRow.Cells["bType"].Value);
                if (!Common.IsDgvCellNull(dgvRow.Cells["target"].Value))
                {
                    TB_Clients c = clients.SingleOrDefault(p => p.id == Convert.ToInt32(dgvRow.Cells["target"].Value));
                    if (c != null)
                        dgvRow.Cells["targetName"].Value = c.fullName;                        
                }
            }
        }
    }
}
